package com.feng.oauth2jwt;

import com.alibaba.druid.pool.DruidDataSource;

import com.feng.oauth2jwt.oauth.domain.oauth.CustomJdbcClientDetailsService;
import com.feng.oauth2jwt.oauth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;

@Configuration
@EnableWebSecurity(debug = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DruidDataSource dataSource;
    @Autowired
    @Qualifier("ouathUserService")
    private UserService ouathUserService;

    @Autowired
    private CustomJdbcClientDetailsService customJdbcClientDetailsService;
//    @Autowired
//    private DefaultTokenServices tokenService;


    @Override
    public void configure(WebSecurity web) throws Exception {
//        web.ignoring().
//                antMatchers("/oauth/token").
//                antMatchers("/oauth/authorize")
//        .antMatchers("test/test");
//        web.ignoring().antMatchers("/login");
       // web.debug(true);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin().and()
                .requestMatchers()
                .antMatchers("/login","/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated().and().userDetailsService(ouathUserService).csrf().disable().anonymous().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);
//        http.authorizeRequests()
//                .antMatchers("/login","/oauth/authorize").
//                .anyRequest().hasAnyRole("ROLE_CLIENT")
//                .and().formLogin()
////                .successHandler(null).failureHandler(null)
//                .and().logout().permitAll()
//                .and().csrf().disable();
//        http.authorizeRequests().antMatchers("/test/test3").authenticated().and().csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(ouathUserService).passwordEncoder(new BCryptPasswordEncoder());
    }
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
